<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * This is the key class to all the admin side data nodes.
    * It is extended into child classes which do all the work.
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 25.05.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class adminNodePage
    ****
    * This class handles the editing of simple HTML pages in INA
    ****
    * @package adminNodes
    * @author tb@essentialmind.com
    * @version n/a
    * @since 13.07.2011
    * @copyright essentialmind.com 2011
    ***/
    class adminNodeExtensibleForm extends adminNode{
        
        function process(){
            if($_POST){
				$this->update();                
			}else{   
				$this->edit();
			}
        }
        
        
        protected function edit() {

        	$this->smarty->assign('formFiles', $this->formFiles());	
			$this->smarty->assign('node',
									$this->db->getRow("SELECT 
																objectmap.title, 
																xml_document_file
														FROM objectmap
														LEFT JOIN node_extensible_form 
															ON objectmap.objectid = node_extensible_form.id 
														WHERE objectmap.version = 1 
														  AND objectmap.nodeid = " . $this->noderef));
		}
        
        protected function update() {    				
        
			$this->db->Execute("	UPDATE node_extensible_form SET
							xml_document_file = " . $this->db->qstr($_POST['xml_document_file']) . "
						WHERE id = " . $this->noderef);
        							
        	//updates the status of the page (title, wf state)
			$this->updatePageStatus();
			
			header('Location: '.$this->request.'&confirm=true');
			exit;						
        }
        
        protected function formFiles() {
        
        	$formFiles = array();
        
        	if ($handle = opendir(ROOT . 'templates/forms/')) {
			
				/* This is the correct way to loop over the directory. */
				while (false !== ($file = readdir($handle))) {
					
					$str  = ".html";
					if ( substr( $file, strlen( $file ) - strlen( $str ) ) == $str ) {
						$formFiles []= $file;
					}
					
				}
			
				closedir($handle);
			}

			return $formFiles;
        }
        
        
        /**
         * Will set the HTML template needed for
         * the child's operation
         * 
         * @return 
         **/
        function setTemplate(){
            $this->setActionTemplate('nodes/editExtensibleForm.html');
        }
        
        
		/** 
         * method which will handle a default add action to the
         * dedicated table
         * 
         * @return integer ID of new object
         * @see ajaxAddPage
         * @see ajaxAddNode
         **/
        function addInstance(){
        
        	$formFiles = $this->formFiles();
        	$firstFile="";
        	
        	if (count($formFiles) > 0) 
        		$firstFile = $formFiles[0];
        		
            $this->db->Execute("INSERT INTO node_extensible_form(xml_document_file) VALUES (".$this->db->qstr($firstFile).")");
            return $this->db->getOne("SELECT last_value FROM node_extensible_form_id_seq");
        }
    }
?>